/**
 * Created by ahwwl on 17-7-28.
 */


function in_array(search,array){
    for(var i in array){
        if(array[i]===search){
            return true;
        }
    }
    return false;
}
function get_card_type(value)
{
    if(value === 0){
        return "中石化普通卡";
    }
    if(value===1){
        return "中石化专属卡";
    }

    if(value === 2){
        return "中石化柴油专属卡";
    }
}
function get_product_name(row)
{
    var basic_price = (row.basic_price * 0.0001).toFixed(2);
    if(row.mode===1){
        return "连连惠开卡"+basic_price+"元";
    }else{
        return "连连惠充值"+basic_price+"元";
    }
}
function get_mode(value)
{
    if(value===1){
        return "开卡";
    }else{
        return "充值";
    }
}
function get_pay_status(value)
{
    if(value===1){
        return "<span style='color:blue;'>已付款</span>";
    }
    if(value === 0){
        return "未付款";
    }
    if(value === 2){
        return "<span style='color:red;'>付款失败</span>";
    }
    if(value === 3){
        return "<span style='color:red;'>已退款</span>";
    }
    if(value === 4){
        return "<span style='color:red;'>已取消</span>";
    }
}
function get_reply_status(value)
{
    if(value===1){
        return "回调中";
    }else if(value===2){
        return "<span style='color:blue;'>回调成功</span>";
    }else if(value===3){
        return "<span style='color:red;'>回调失败</span>";
    }else{
        return "未回调";
    }
}
function get_status(value)
{
    if(value===1){
        return "充值中";
    }else if(value===2){
        return "<span style='color:blue;'>充值成功</span>";
    }else if(value===3){
        return "<span style='color:red;'>充值失败</span>";
    }else{
        return "未处理";
    }
}
function get_process_flag(value)
{
    if(value===1){
        return "处理中";
    }else if(value===2){
        return "<span style='color:blue;'>提交成功</span>";
    }else if(value===3){
        return "<span style='color:red;'>提交失败</span>";
    }else{
        return "未处理";
    }
}

function get_delivery_status(value)
{
    if(value===1){
        return "未发货";
    }else if(value===2){
        return "发货中";
    }else if(value===3){
        return "发货完成";
    }else{
        return "无发货";
    }
}
function get_token_type(value)
{
    if(value===1){
        return "微信";
    }else if(value===2){
        return "小程序";
    }else if(value===3){
        return "API";
    }else{
        return "WEB";
    }
}
function pay_type(value)
{
    if(value===1){
        return "微信";
    }else if(value===2) {
        return "支付宝";
    }else if(value === 3){
        return "预存款";
    }else if(value === 4){
        return "代理预存款";
    }else if(value === 5){
        return "企业预存款";
    }else if(value === 6){
        return "测试款";
    }else{
        return "手工付款";
    }

}
function getLocalTime(nS) {
    if(!nS)return "0";
    return new Date(parseInt(nS) * 1000).Format("yyyy-MM-dd hh:mm:ss");
}
function openMsg(content)
{
    window.top.$.ley.msg({content:content});
}
function openMsgS(s,callbak)
{
    window.top.$.ley.prompt("<div style='padding:10px;'>"+s+"</div>",function () {
        callbak();
    });
}
function delMsg(callbak)
{
    // openDialog1({title:"提示","content":"<div style='padding:10px;'>是否确认删除?</div>",area:["300px"],yes: function (index) {
    //     callbak();
    //     window.top.layer.close(index);
    // }})
    window.top.$.ley.prompt("<div style='padding:10px;'>是否确认删除?</div>",function () {
        callbak();
    });

}
function promptMsg(title,callbak)
{
    $("body>.dropdown-menu").remove();
    window.top.$.ley.prompt("<div style='padding:10px;'>"+title+"</div>",function () {
        callbak();
    });
}
function openDialog(s)
{
    // var scroll_pos = $(parent.window).scrollTop();
    // $(parent.window).scrollTop(0);
    var h = $(window).height();
    if(window.top !== window.self)
    {
        h = $(window.top).height();
    }
    h= h-100;
    var default_s = {
        type: 1,
        title: '提示',
        shadeClose: true,
        shade: 0.3,
        area: ['100%', h+"px"],
        offset:"t",
        content: '',
        btn: ['确定','取消'],
        zIndex: layer.zIndex,
        yes: function (index) {
            layer.close(index);
        },
        cancel: function (index) {
            layer.close(index);
        },
        success: function (layero) {
            $("#style_edit").remove();
            $("<div id='style_edit' style='display:none;'><style></style></div>").appendTo($("body"));
            $("#style_edit style").html('.select2-container--open{z-index:'+(layer.zIndex+1000)+'}')
            layer.setTop(layero);

            // dialog_success();
        },
        end:function () {
            // $(parent.window).scrollTop(scroll_pos);
        }
    };
    s = $.extend({},default_s,s);
    layer.open(s);
    var scroll_pos = $(parent.window).scrollTop()-140;
    if(scroll_pos<0)
    {
        scroll_pos = 0;
    }
    $('.layui-layer').css({top: scroll_pos});
}
function openDialog1(s)
{
    // var scroll_pos = $(parent.window).scrollTop();
    // $(parent.window).scrollTop(0);
    if(s.info_obj)
    {
        var edit_info_obj = s.info_obj.clone(true,true);
        edit_info_obj.appendTo($("body",parent.document));
        s.info_obj = edit_info_obj;
        $("select",edit_info_obj).formSelect();
        s.content = edit_info_obj;

    }
    var _parent = window.top;
    var default_s = {
        type: 1,
        title: '提示',
        shadeClose: true,
        shade: 0.3,
        area: ['100%', "100%"],
        content: '',
        btn: ['确定','取消'],
        zIndex: _parent.layer.zIndex,
        yes: function (index,layero) {
            _parent.layer.close(index);
        },
        cancel: function (index) {
            _parent.layer.close(index);
        },
        success: function (layero) {
            $("#style_edit").remove();

            _parent.layer.setTop(layero);
            if(s.before)
            {
                s.before(layero)
            }
            // dialog_success();
        },
        end:function () {
            // $(parent.window).scrollTop(scroll_pos);
            if(s.info_obj)
            {
                s.info_obj.remove();
            }
        }
    };
    s = $.extend({},default_s,s);
    _parent.layer.open(s);

}

function openDialog2(s)
{
    // var scroll_pos = $(parent.window).scrollTop();
    // $(parent.window).scrollTop(0);

    // var _parent = window.top;
    // var default_s = {
    //     type: 1,
    //     title: '提示',
    //     shadeClose: true,
    //     shade: 0.3,
    //     area: ['100%', "100%"],
    //     content: '',
    //     btn: ['确定','取消'],
    //     zIndex: _parent.fDialog.zIndex,
    //     yes: function (index,layero) {
    //         _parent.fDialog.close(index);
    //     },
    //     cancel: function (index) {
    //         _parent.fDialog.close(index);
    //     },
    //     success: function (layero) {
    //
    //         // _parent.fDialog.setTop(layero);
    //         if(s.before)
    //         {
    //             s.before(layero)
    //         }
    //         // dialog_success();
    //     },
    //     end:function () {
    //         // $(parent.window).scrollTop(scroll_pos);
    //     }
    // };
    // s = $.extend({},default_s,s);
    // _parent.fDialog.open(s);
    var _parent = window.top;
    var default_s = {
        success: function (layero) {
            layero.find(".ley-dialog-body>div").show();
            $("body>.dropdown-menu").remove();
            if(s.before)
            {

                s.before(layero)
            }
        }
    };
    s = $.extend({},default_s,s);
    _parent.$.ley.open(s);

}


function loading(is_show)
{
    var obj = $("#preloader");
    if(window.top!==window.self)
    {
        obj = $('#preloader', window.top.document);
    }

    if(is_show)
    {
        obj.show();
    }else{
        obj.hide();
    }
}
function AjaxCommen(type,url,data,callback,dataType) {
    if (!dataType)
    {
        dataType = "json";
    }
    var info = {
        type: type,
        url: url,
        data:data,
        dataType: dataType,
        success:function (msg) {
           if(msg)
           {
               check_table_list_data(msg);
               callback(msg);
               loading(false);
           }else{
               loading(false);
               openMsg("error");
           }


        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {

            loading(false);
            openMsg("http status error:"+errorThrown)
        }
    };
    if(dataType === "jsonp")
    {
        info["jsonp"] = 'jsoncallback';
    }

    loading(true);
    $.ajax(info);
}
function check_table_list_data(data)
{
    var time = 1000;
    if(data.url)
    {
        setTimeout(function () {
            if(window.top!==window.self)
            {
                top.location.href = data.url;
            }else{
                location.href=data.url;
            }
        },time)
    }
    if(data.error)
    {
        openMsg(data.msg);
    }
    loading(false);
    // check_parent_height();
}

function make_edit_table(obj)
{
    var content = "";
    var type = obj.attr("data-type");
    var callback = obj.attr("data-callback");
    var is_value = obj.attr("data-is_value");
    var name = obj.attr("data-name");
    var value = "";
    if(is_value === "true")
    {
        value = obj.attr("data-value");
    }else{
        value = obj.html();
    }
    var content_obj = null;
    var parems = {};
    if(obj[0].parems)
    {
        parems = obj[0].parems;
    }
    if(type === "text")
    {
        content_obj = $('<div><div style="margin:10px;"><input type="text" style="width:100%;" class="layui-input" name="value" value="'+value+'"></div></div>');

        content = content_obj.html();
    }
    openDialog({title:"编辑",content:content,area:["300px","160px"],yes:function (index,layero) {
        var is_write = true;
        var value = layero.find("input").val();
        if(callback && callback.length>0)
        {
            callback = eval(callback);
            if($.type(parems) !== "object")
            {
                parems = {};
            }
            if($.isFunction(callback))
            {
                var cbak = function (obj1,index) {
                    var is_value1= obj1.attr("data-is_value");
                    if(is_value1 === "true")
                    {

                        obj1.attr("data-value",value);
                    }else{
                        obj1.html(value);
                    }

                    layer.close(index);
                };
                callback(name,value,parems,obj,index,cbak);
            }

        }else if(is_write)
        {

            if(is_value === "true")
            {

                obj.attr("data-value",value);
            }else{
                obj.html(value);
            }

            layer.close(index);
        }

    }});

}


function get_checkbox_values(obj)
{
    var list = [];
    for(var i=0;i<obj.length;i++)
    {
        list.push(obj.eq(i).val());
    }
    return list;
}
// document.body.onresize = function () {
//     console.log("aaa")
// };
// // window.resizeBy(100, 100);
// $(document).ready(function () {
//     if(window.top!=window.self)
//     {
//         $(window).load(function () {
//             console.log($(window).height());
//         })
//         // var frame_obj = $(window.frameElement) ;
//         // // frame_obj.parent().height(1000)
//         // // $("body").height(1000);
//         // console.log(frame_obj.height())
//     }
//
// });
function check_parent_height()
{

    // if (window.frames.length != parent.frames.length) { alert('在iframe中'); }
    if(window.top!=window.self)
    {
        var h = $("body").height()+200;
        if(h <500)
        {
            h = 500;
        }
        var name = $(window.frameElement).attr("name");
        if(!name)
        {
            $(window.frameElement).height(h);
        }
        else if(name !=="layui-layer-iframe2" ||name !== 'layui-layer-iframe1')
        {

        }else{
            $(window.frameElement).height(h);
        }

        $(".mainpanel",top.window.document.body).css("height","auto");
    }
}



Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};
(function (original) {
    jQuery.fn.clone = function () {
        var result = original.apply(this, arguments),
            my_textareas = this.find('textarea').add(this.filter('textarea')),
            result_textareas = result.find('textarea').add(result.filter('textarea')),
            my_selects = this.find('select').add(this.filter('select')),
            result_selects = result.find('select').add(result.filter('select'));

        for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
        for (var i = 0, l = my_selects.length; i < l; ++i) result_selects[i].selectedIndex = my_selects[i].selectedIndex;

        return result;
    };
}) (jQuery.fn.clone);



$(function () {

    // .layui-layer
    // if(window.top != window.self)
    // {
    //     $(parent).scroll(function() {
    //
    //         var scroll_pos = $(parent).scrollTop()-140;
    //         if(scroll_pos<0)
    //         {
    //             scroll_pos = 0;
    //         }
    //         if($(".layui-layer").attr("type") == "page")
    //         {
    //             $('.layui-layer').css({top: scroll_pos});
    //         }
    //     });
    //
    //
    // }
    $("body").delegate(".sb_amount","blur",function () {

        var obj = $(this).parents("#edit_info");
        var value = $(this).val();
        var data_value = $(this).attr("data-value");
        if(obj)
        {

            var bond_amount = obj.find(".bond_amount").val();
            if(bond_amount&&!isNaN(value)&&!isNaN(data_value)&&!isNaN(bond_amount)&&bond_amount>0&&value>0)
            {
                obj.find(".bond_amount").val(bond_amount/data_value*value);
                $(this).attr("data-value",value);
            }
        }
    });
    $("body").delegate(".bond_currency","change",function () {

        var obj = $(this).parents(".ley-dialog-body");
        var value = $(this).val();
        var data_value = $(this).attr("data-value");
        if(obj)
        {

            var bond_amount = obj.find(".bond_amount").val();
            if(bond_amount&&data_value!==value&&value)
            {
                if(value === "HK$")
                {
                    bond_amount = bond_amount*1.2;
                }else{
                    bond_amount = bond_amount/1.2;
                }
                obj.find(".bond_amount").val(bond_amount);
                $(this).attr("data-value",value);
            }
        }
    });
    $("body").delegate(".search_tools>.search_toogle","click",function () {
        if($("span",this).hasClass("glyphicon-chevron-down"))
        {
            $("span",this).removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up");
            $(this).prev("#search_form").show();
        }else{
            $("span",this).addClass("glyphicon-chevron-down").removeClass("glyphicon-chevron-up");
            $(this).prev("#search_form").hide();
        }
    });



});